﻿//图片列表
function getImgs(imgList) {

    var html = "";
    if (imgList.length == 1) {
        
        if (imgList == null || trim(imgList[0]) == '') return "";
        html += "<img class='list-img list-img-1' data-all='" + imgList[0] + "' data-src='" + imgList[0] + "' data-index ='0' src='" + siteConfig.formatBlogImage(imgList[0]) + "' style='height: auto;' onclick='viewImg()'>";
    } else {
        for (var i = 0; i < imgList.length; i++) {
            var src = imgList[i]
            if (src == null || trim(src) == '') continue;
            var clearfloat = imgList.length == 4 && i == 2 ? " clearfloat" : "";
            html += "<div class='list-img list-img-3"+clearfloat+"' data-all='" + imgList + "'  data-src='" + src + " 'data-index ='" + i + "'  ><img src='" + siteConfig.formatBlogImage(src) + "' style='width:100%;height: auto;' onclick='viewImg()'></div>"
        }
    }
    return html;
}



//获得点赞列表
function getUpList(upList) {
    if (upList == null) return "";
    var html = "";
    for (var i = 0; i < upList.length; i++) {
        if (i > 0)
            html += " ,"
        html += upList[i].NickName;
    }
    return html;
}

//获得评论列表
function getCommentList(commentList) {
    if (commentList == null) return "";
    var html = ''
    for (var i = 0; i < commentList.length; i++) {
        var item = commentList[i]
        html += '<p><span>' + item.NickName + '：</span>' + item.Content + '</p>'
    }
    return html;
}

//删除动态
function deleteBlog(blogId) {
    confirmDialog("确认删除吗", function () {
        ajaxPost(siteApiList.DeleteBlog, "blogId=" + blogId + "&storeid=" + storeid, function (data) {
            Alert('删除成功');
            loadBlog();
        })
    });
}

//点赞
function upAdd(blogId) {
    ajaxPost(siteApiList.AddUp, "uid=" + userid + "&blogId=" + blogId, function (data) {
        loadBlog();
    })
}

//评论
function blogAdd() {
    var blogId = tailkBoid;
    var content = $("#bloginput").val();
    if (content == "") {
        Alert("评论的内容不能为空！");
        return false;
    }
    ajaxPost(siteApiList.AddBlogComment, "uid=" + userid + "&blogId=" + blogId + "&content=" + content, function (data) {
        loadBlog();
        $(".tabbar").show();
        $(".sendMsg").hide();
        $("#bloginput").val("")
    })
}

function Trim(str)
 { 
  return str.replace(/(^\s*)|(\s*$)/g, ""); 
}
//预览图片
function viewImg() {
    $('.list-img').on('click', function () {
        var src = Trim(siteConfig.formatBlogImage($(this).attr('data-src')));
        var allSrc = $(this).attr('data-all').split(",");
        var index = $(this).attr('data-index');
        for (var i = 0; i < allSrc.length; i++) {
            allSrc[i] = Trim(siteConfig.formatBlogImage(allSrc[i].replace(/[\r\n]/g, "")));
        }
        console.log(src)
        var gallery = weui.gallery(src, {
            className: 'custom-classname',
            onDelete: function () {
                if (confirm('确定删除该图片？')) {
                }
            }
        })
        var page = parseInt(index) + 1;
        var showNum = "<div class='showNum'>" + page + "/" + allSrc.length + "</div>"
        $('.weui-gallery__img').html(showNum)
        var mybody = $('.weui-animate-fade-in')[0];
        var startX, startY, moveEndX, moveEndY, X, Y;
        mybody.addEventListener('touchstart', function (e) {
            e.preventDefault();
            startX = e.touches[0].pageX;
            startY = e.touches[0].pageY;
        }, false);
        mybody.addEventListener('touchend', function (e) {
            e.preventDefault();
            moveEndX = e.changedTouches[0].pageX;
            moveEndY = e.changedTouches[0].pageY;
            X = moveEndX - startX;
            Y = moveEndY - startY;
            if (X > 80) {
                // alert("向右");
                if (index - 1 >= 0) {
                    $('.weui-gallery__img').css('background-image', "url(" + allSrc[index - 1] + ")");
                    $('.showNum').html(page - 1 + "/" + allSrc.length)
                    page--;
                    index--;
                } else {
                    index = allSrc.length;
                    $('.weui-gallery__img').css('background-image', "url(" + allSrc[index - 1] + ")");
                    page = allSrc.length;
                    $('.showNum').html(page + "/" + allSrc.length)
                    index--;
                }
            } else if (X < -80) {
                if (parseInt(index) + 1 <= allSrc.length - 1) {
                    index++;
                    page++;
                    $('.weui-gallery__img').css('background-image', "url(" + allSrc[index] + ")");
                    $('.showNum').html(page + "/" + allSrc.length);
                } else {
                    index = -1;
                    page = 1;
                    index++;
                    $('.weui-gallery__img').css('background-image', "url(" + allSrc[index] + ")");
                    $('.showNum').html(page + "/" + allSrc.length);
                }
            } else if (X == 0) {
                // $('.weui-gallery').css('display', 'none');
                gallery.hide(function () {
                    console.log('`gallery` has been hidden');
                });
            }
        });
    });
}